#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

void Swap(int* px, int* py)
{
	int tmp = *px;
	*px = *py;
	*py = tmp;
}

void BubbleSort(int* a,int size)
{
	int end = size;
	while (end > 0)
	{
		int change = 0;
		for (int i = 0; i < end-1; i++)
		{
			if (a[i] > a[i + 1])
			{
				//如果遍历时交换过则记录
				change = 1;
				Swap(&a[i], &a[i + 1]);
			}
		}
		//如果遍历过一遍没有改变过说明已经排好，无需排序;
		if (change == 0)
		{
			break;
		}
		end--;
	}
}

int main()
{
	int a[] = { 9,8,7,6,5,4,3,2,1 };
	BubbleSort(a, 9);
	for (int i = 0; i < 9; i++)
	{
		printf("%d ", a[i]);
	}
	return 0;
}